import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '@/layout/MainLayout.vue'
import Login from '@/views/Login.vue'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: {
                title: '登录',
                requiresAuth: false
            }
        },
        {
            path: '/',
            component: MainLayout,
            redirect: '/dashboard',
            meta: {
                requiresAuth: true
            },
            children: [
                {
                    path: 'dashboard',
                    name: 'Dashboard',
                    component: () => import('@/views/dashboard/Dashboard.vue'),
                    meta: {
                        title: '首页',
                        keepAlive: true,
                        requiresAuth: true
                    }
                },
                {
                    path: 'user/list',
                    name: 'UserList',
                    component: () => import('@/views/user/UserList.vue'),
                    meta: {
                        title: '用户管理',
                        keepAlive: true,
                        requiresAuth: true
                    }
                },
                {
                    path: 'teacher/list',
                    name: 'TeacherList',
                    component: () => import('@/views/teacher/TeacherList.vue'),
                    meta: {
                        title: '教师管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'student/list',
                    name: 'StudentList',
                    component: () => import('@/views/student/StudentList.vue'),
                    meta: {
                        title: '学生管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'class/list',
                    name: 'ClassList',
                    component: () => import('@/views/class/ClassList.vue'),
                    meta: {
                        title: '班级管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'course/list',
                    name: 'CourseList',
                    component: () => import('@/views/course/CourseList.vue'),
                    meta: {
                        title: '课程管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'score/list',
                    name: 'ScoreList',
                    component: () => import('@/views/score/ScoreList.vue'),
                    meta: {
                        title: '成绩管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'selection/list',
                    name: 'SelectionList',
                    component: () => import('@/views/selection/SelectionList.vue'),
                    meta: {
                        title: '选课管理',
                        requiresAuth: true
                    }
                },
                {
                    path: 'profile',
                    name: 'Profile',
                    component: () => import('@/views/user/Profile.vue'),
                    meta: {
                        title: '个人信息',
                        requiresAuth: true
                    }
                },
                {
                    path: 'password',
                    name: 'Password',
                    component: () => import('@/views/user/ChangePassword.vue'),
                    meta: {
                        title: '修改密码',
                        requiresAuth: true
                    }
                },
                {
                    path: 'schedule/list',
                    name: 'ScheduleList',
                    component: () => import('@/views/schedule/ScheduleList.vue'),
                    meta: {
                        title: '课表管理',
                        requiresAuth: true
                    }
                }
            ]
        },
        {
            path: '/:pathMatch(.*)*',
            name: 'NotFound',
            component: () => import('@/views/error/404.vue'),
            meta: {
                title: '404',
                requiresAuth: false
            }
        }
    ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    document.title = to.meta.title ? `${to.meta.title} - 课程管理系统` : '课程管理系统'

    const token = localStorage.getItem('token')

    if (to.meta.requiresAuth) {
        if (!token) {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        } else {
            next()
        }
    } else {
        if (token && to.path === '/login') {
            next('/dashboard')
        } else {
            next()
        }
    }
})

export default router